﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace SERIX.Controles.Report
{
    public partial class Planejamento_programas_ano : System.Web.UI.UserControl
    {
        #region Variaveis

        private List<int> programas;

        #endregion

        #region Propriedades

        public string Ano
        {

            set
            {
                programas = new List<int>();
                ManipulaLabel();
                SqlDataSource1.SelectParameters["ano"].DefaultValue = value;
                SqlDataSource1.DataBind();
                GridView1.DataBind();
            }
        }

        public List<int> Programas
        {
            //set { this.programas = value; }
            get { return this.programas; }
        }

        #endregion

        #region Eventos

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Session["sessao"] = this.programas;
            }
            else
            {
                this.programas = (List<int>)Session["sessao"];
            }
            ManipulaLabel();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                CheckBox cb = (CheckBox)e.Row.Cells[0].Controls[1];
                string valor = ((DataRowView)e.Row.DataItem)["idt_prog"].ToString();
                cb.Attributes.Add("key", valor);

                for (int i = 0; i < programas.Count; i++)
                {
                    if (valor == programas[i].ToString())
                    {
                        cb.Checked = true;
                    }
                }
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            VerificaGrid();
            Panel1_ModalPopupExtender.Hide();
            ManipulaLabel();
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            VerificaGrid();
            Panel1_ModalPopupExtender.Show();
        }

        #endregion      

        #region Métodos

        protected void ManipulaLabel()
        {
            if (this.programas.Count == 0)
            {
                Label1.Text = "Todos";
            }
            else
            {
                Label1.Text = this.programas.Count.ToString() + " Selecionados";
            }
        }

        protected void VerificaGrid()
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox cb = (CheckBox)row.Cells[0].Controls[1];
                if (cb.Checked)
                {
                    bool inserir = true;
                    for (int i = 0; i < this.programas.Count; i++)
                    {
                        if (this.programas[i].ToString().Equals(cb.Attributes["key"]))
                        {
                            inserir = false;
                        }
                    }
                    if (inserir)
                    {
                        this.programas.Add(int.Parse(cb.Attributes["key"]));
                    }
                }
                else
                {
                    try
                    {
                        this.programas.Remove(int.Parse(cb.Attributes["key"]));
                    }
                    catch
                    {

                    }
                }
            }
            //Session["sessao"] = this.programas;
        }

        #endregion
    }
}